<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="../../css/layui.css">
    <script src="/js/layui/layui.all.js"></script>
    <script src="/admin/js/token.js"></script>
</head>
<body>

<script type="text/html" id="caozuo">
    <button type="button" class="layui-btn" lay-event="detail">授权密钥</button>


</script>

<table class="layui-table" id="terminaldata" lay-filter="terminaldata"></table>
</body>
</html>
<script type="text/javascript">

    // var

        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,table = layui.table
            , layer = layui.layer
            , util = layui.util;

        var s=layui.data("s");
        var tid=s.terminal.terminal_id;
        var stationBh=getUrlSearch("stationBh");
    function getUrlSearch(name) {
        // 未传参，返回空
        if (!name) return null;
        // 查询参数：先通过search取值，如果取不到就通过hash来取
        var after = window.location.search;
        after = after.substr(1) || window.location.hash.split('?')[1];
        // 地址栏URL没有查询参数，返回空
        if (!after) return null;
        // 如果查询参数中没有"name"，返回空
        if (after.indexOf(name) === -1) return null;

        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        // 当地址栏参数存在中文时，需要解码，不然会乱码
        var r = decodeURI(after).match(reg);
        // 如果url中"name"没有值，返回空
        if (!r) return null;

        return r[2];
    }
        table.render({
            elem: '#terminaldata'
            , id: "t"
            , toolbar: "#tol"
            , url: '/admin/terminal-key/selectPassword?station_id='+stationBh+"&terminal_id="+tid
            ,method:"get"

            , cols: [[
                {
                    field: 'terminal_key_id', title: '密钥ID', width: "24%",
                    align: "center"
                }
              /*  ,
                {
                    field: '', title: 'Mac地址',
                    // width: "25%",
                    align: "center"
                }*/
                , {
                    field: 'terminal_key_value',
                    // width: "24%",
                    align: "center", title: '密钥'
                }
                , {
                    title:'操作',
                    align:'center',
                    toolbar:'#caozuo',
                    fixed: 'right'
                }
            ]],
            page: true
        });

        table.on('tool(terminaldata)',function(obj){

            var d = new Date();
            var year=  d.getFullYear();

            var month=  d.getMonth()+1;
            var startDate=  d.getDate();
            var endDate=  d.getDate()+1;
            var hours= d.getHours();
            var minutes= d.getMinutes();
            var seconds= d.getSeconds();
            var startTime= year+"-"+month+"-"+startDate+" "+hours+":"+minutes+":"+seconds;
            var endTime= year+"-"+month+"-"+endDate+" "+hours+":"+minutes+":"+seconds;


            var getdata=obj.data; //获取当前行数据
            var layEvent=obj.event; //获取lay-event对应的值,(也可以是表头的event参数对应的值)
            var tr=obj.tr; //获取当前行tr的DOM对象(如果有的话)
            switch(layEvent){

                   case "detail":
//询问框

                    layer.confirm('你确定要给该终端设置密钥吗？', {
                        btn: ['标准型','终点型','取消'] //按钮
                    }, function(){

                            $.ajax({
                                url:"/admin/terminal-key/usePasswordKey",
                                type:"get",
                                data:{
                                    terminal_key_id:getdata.terminal_key_id,
                                    terminal_id:tid,
                                    terminal_key_value:getdata.terminal_key_value,
                                    terminal_key_begin_time:startTime,
                                    terminal_key_end_time:endTime
                                }
                            })
                        layer.msg('设置成功！', {icon: 1,time: 1000
                                ,
                            end:function () {
                                localStorage.setItem("ifclone","1");
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                //layer.msg(index);
                                parent.layer.close(index); //再执行关闭
                                parent.location.reload();
                            }
                        });
                        //     layer.msg(dateTime, {icon: 1});

                        }, function(){
                            layer.open({
                                type: 1,
                                title: "请设置到期时间",
                                closeBtn: 0,
                                shadeClose: true,
                                area: ['500px', '350px'],
                                skin: 'yourclass',
                                content: '<div class="layui-fluid">\n' +
                                    '  <div class="layui-form-item ">\n' +
                                    '    <div class="layui-inline ">\n' +
                                    '      <label class="layui-form-label" >到期时间:</label>\n' +
                                    '      <div class="layui-input-inline">\n' +
                                    '        <input type="text" id="end" lay-verify="date" style="width: 250px" autocomplete="off" class="layui-input">\n' +
                                    '      </div>\n' +
                                    '    </div>\n' +
                                    '  </div>\n' +
                                    '\n' +
                                    '  <div>\n' +
                                    '    <span style="color: red;margin-left: 100px">*右击左下角选择时间可选择时分秒</span>\n' +
                                    '  </div>\n' +
                                    '  <br>\n' +
                                    '  <div class="layui-inline">\n' +
                                    '    <button type="button" class="layui-btn" style="margin-left: 250px" id="queding">确认</button>\n' +
                                    '  </div>\n' +
                                    '\n' +
                                    '</div>',
                                success: function(){
                                    laydate.render({

                                        elem: "#end",
                                        type: 'datetime',
                                        trigger: 'click',
                                        done: function(value, date, endDate) {

                                            var endtime = new Date(value).getTime();
                                            if(endtime < startTime) {

                                                alert("no")
                                            }
                                        }

                                    });
                                    $(document).on('click', '#queding', function() {
                                        var endtime =$("#end").val();
                                        if (endtime<startTime){
                                            layer.msg('结束时间不能小于当前时间'),{icon: 1,time: 1000};
                                            return;
                                        }
                                        $.ajax({
                                            url:"/admin/terminal-key/usePasswordKey",
                                            type:"get",
                                            data:{
                                                terminal_key_id:getdata.terminal_key_id,
                                                terminal_id:tid,
                                                terminal_key_value:getdata.terminal_key_value,
                                                terminal_key_begin_time:startTime,
                                                terminal_key_end_time:endtime
                                            }
                                        })
                                        layer.msg('设置成功！', {icon: 1,time: 1000
                                            ,
                                            end:function () {
                                            localStorage.setItem("ifclone","1");
                                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                //layer.msg(index);
                                                parent.layer.close(index); //再执行关闭
                                                parent.location.reload();
                                            }
                                        });


                                    });
                                },
                                error : function() {
                                    alert("操作失败！");
                                }

                            });
                    },function () {
                        layer.close();
                        }

                    );
                     break;


            }
        });


        $("#Searchbtn").click(function(){
            table.reload("t",{
                where:{
                    startTime:$("#startTimeval").val(),
                    endTime:$("#endTimeval").val()
                },
                page:{
                    curr:1
                }
            });
        });


        //日期
        laydate.render({
            elem: '#startTimeval'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#endTimeval'
            ,type: 'datetime'
        });






</script>

<!--table日期转换格式-->
<script>
    function Format(datetime,fmt) {
        if (parseInt(datetime)==datetime) {
            if (datetime.length==10) {
                datetime=parseInt(datetime)*1000;
            } else if(datetime.length==13) {
                datetime=parseInt(datetime);
            }
        }
        datetime=new Date(datetime);
        var o = {
            "M+" : datetime.getMonth()+1,                 //月份
            "d+" : datetime.getDate(),                    //日
            "h+" : datetime.getHours(),                   //小时
            "m+" : datetime.getMinutes(),                 //分
            "s+" : datetime.getSeconds(),                 //秒
            "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
            "S"  : datetime.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }
</script>


